package com.example.blog.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class SecurityUtil {

    public static String encrypt(String password){      //对密码进行加密

        //生成一个盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        System.out.println(salt);

        //最终密码=md5（盐值 + 原始密码）
        String finalPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
//        System.out.println(finalPassword);
//        System.out.println(salt+finalPassword);
        return finalPassword+salt;
    }


    public static boolean verify(String password , String finalPassword){
//        String password = "123456";
//        String finalPassword ="a6dba64f4dc04af590ab8854586eef3bdfd6cf95b2430b90e21962f48c47524d";

        if(!StringUtils.hasLength(password)||!StringUtils.hasLength(finalPassword)){
            System.out.println(true);
            //return false;
        }
        String salt = finalPassword.substring(0,32);

        String securityPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return (salt + securityPassword).equals(finalPassword);

    }

}
